#region Usings
using UnityEngine;
#endregion

/// <summary>
/// Hero movement
/// </summary>
public class HeroMovement : MonoBehaviour
{
    #region Properties and Fields
    /// <summary>
    /// Direction of the player
    /// </summary>
    private Statics.Direction m_Direction = Statics.Direction.Left;

    /// <summary>
    /// Strafe direction of the player
    /// </summary>
    private Statics.Direction m_StrafeDirection = Statics.Direction.Left;

    /// <summary>
    /// Velocity of the player
    /// </summary>
    private Vector3 m_vVelocity = Vector3.zero;
    #endregion

    #region Functions
    /// <summary>
    /// FixedUpdate function
    /// </summary>
    public void FixedUpdate()
    {
        //Get rigidbody
        Rigidbody rigidBody = GetComponent<Rigidbody>();

        //Velocity vector
        Vector3 velocity = Vector3.zero;

        //Get movement
        velocity.x = Input.GetAxis("Horizontal");
        velocity.z = Input.GetAxis("Vertical");

        //Calc new position
        Vector3 newPos = transform.position + (velocity * Time.deltaTime);

        //Move object
        rigidBody.MovePosition(newPos);

        //Set new direction
        //x is negative, face left
        if (velocity.x < 0 && velocity.z == 0)
        {
            //Strafe is pressed
            if(Input.GetButton("Strafe"))
                //Strafe left
                m_StrafeDirection = Statics.Direction.Left;

            else
                //Move left
                m_Direction = Statics.Direction.Left;
        }

        //x is positive, face right
        else if (velocity.x > 0 && velocity.z == 0)
        {
            //Strafe is pressed
            if (Input.GetButton("Strafe"))
                //Strafe right
                m_StrafeDirection = Statics.Direction.Right;

            else
                //Move right
                m_Direction = Statics.Direction.Right;
        }

        //z is positive, face up
        else if (velocity.x == 0 && velocity.z > 0)
        {
            //Strafe is pressed
            if (Input.GetButton("Strafe"))
                //Strafe up
                m_StrafeDirection = Statics.Direction.Up;

            else
                //Move up
                m_Direction = Statics.Direction.Up;
        }

        //z is negative, face down
        else if (velocity.x == 0 && velocity.z < 0)
        {
            //Strafe is pressed
            if (Input.GetButton("Strafe"))
                //Strafe down
                m_StrafeDirection = Statics.Direction.Down;

            else
                //Move down
                m_Direction = Statics.Direction.Down;
        }

        //Save new velocity
        m_vVelocity = velocity;
    }

    /// <summary>
    /// Update function
    /// </summary>
    public void Update()
    {
        //Get sprite animator
        SpriteAnimator spriteAnim = transform.Find("HeroTile").GetComponent<SpriteAnimator>();

        //See what direction moving to or facing
        switch(m_Direction)
        {
            case Statics.Direction.Left:
                //Is moving
                if(m_vVelocity.x < 0)
                    spriteAnim.AnimateSprite(8, 5, 0, 0, 8, 12);

                //Not moving
                else
                    spriteAnim.AnimateSprite(8, 5, 0, 4, 1, 12);
                break;

            case Statics.Direction.Right:
                //Is moving
                if (m_vVelocity.x > 0)
                    spriteAnim.AnimateSprite(8, 5, 0, 1, 8, 12);

                //Not moving
                else
                    spriteAnim.AnimateSprite(8, 5, 1, 4, 1, 12);
                break;

            case Statics.Direction.Up:
                //Is moving
                if (m_vVelocity.z > 0)
                    spriteAnim.AnimateSprite(8, 5, 0, 2, 8, 12);

                //Not moving
                else
                    spriteAnim.AnimateSprite(8, 5, 2, 4, 1, 12);
                break;

            case Statics.Direction.Down:
                //Is moving
                if (m_vVelocity.z < 0)
                    spriteAnim.AnimateSprite(8, 5, 0, 3, 8, 12);

                //Not moving
                else
                    spriteAnim.AnimateSprite(8, 5, 3, 4, 1, 12);
                break;
        }
    }
    #endregion
}
